﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.ComponentModel;
using Telerik.Web.UI;
using System.Data.Common;


public partial class Admin_Components_Member_ListFunction : System.Web.UI.UserControl
{
    //DatabaseHelper DB = new DatabaseHelper();
    DataTable dt = new DataTable();
    int DetailID = 15;
    string DetailName = "Function";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetupForm();
            grvData.DataSource = dt;
            grvData.DataBind();

            //btnSearch_Click(null, null);
        }
        
    }
    protected void SetupForm()
    {
        //Load dữ liệu cho các control khi vừa load form
        //Utils.LoadDataToCombo(cboParentID, "Function_cbo_Parent",DbType.Int32 ,1);
        ControlUtil.ForComboHierarchy<Function>(cboParentID, "-- Tất cả --", false);
        RadTreeView tvCategory = (RadTreeView)cboParentID.Items[0].FindControl("tvHierarchy");
        tvCategory.Nodes.Insert(1, new RadTreeNode("Không có", "0"));
        cboParentID.SelectedIndex = 0;

        Utils.LoadCodeDetail(cboFunctionType, "FunctionType", 1);
        cboFunctionType.SelectedIndex = 0;

        cboFunctionLevel.SelectedIndex = 0;
        //Hiển thị thông tin các column trên lưới khi load form, lúc này chưa cần load dữ liệu lên lưới
        DataTable dt = new DataTable();
        grvData.DataSource = dt;
        grvData.DataBind();
    }

    protected void ClearForm()
    {
        //txtID.Text = "0";
        //txtID.MinValue = 0;
        txtMethodName.Text = "";
        cboFunctionLevel.SelectedIndex = 0;
        cboFunctionType.SelectedIndex = 0;
        cboParentID.SelectedIndex = 0;
        txtFunctionName.Text = "";
        radAll.Checked = true;
        radSysFunction.Checked = false;
        radPublicFunction.Checked = false;
        DataTable dt = new DataTable();
        grvData.DataSource = dt;
        grvData.DataBind();
    }
    private void LoadDatatoGrid()
    {
        int func = -1;
        if (radSysFunction.Checked)
        {
            func = 1;
        }
        else
        {
            if (radPublicFunction.Checked)
            {
                func = 0;
            }
        }


        int parentID = int.Parse(ControlUtil.ForComboHierarchySelectedValue(cboParentID) == "" ? "-1" : ControlUtil.ForComboHierarchySelectedValue(cboParentID));
        int functionLevel = cboFunctionLevel.SelectedValue.Length != 0 ? int.Parse(cboFunctionLevel.SelectedValue) : -1;

        DBDataContext db = LINQUtil.GetDBReadOnly();
        var q = db.Function_Search(txtMethodName.Text, cboFunctionType.SelectedValue, parentID, txtFunctionName.Text, functionLevel, func).ToList();

        if (q.Count() > 0)
        {
            
            this.grvData.DataSource = q;
            this.grvData.DataBind();
            //this.grvData.Visible = true;
        }
        else
        {
            JavascriptUtil.LoadMessage(UpdatePanel1, false, "Không tìm thấy dữ liệu! ");
            
            this.grvData.DataSource = q;
            this.grvData.DataBind();
            //this.grvData.Visible = true;
        }
    }

    protected void grvData_ItemCommand(object sender, GridCommandEventArgs e)
    {
        //try
        //{
            string commandName = e.CommandName;

            if (commandName == "ViewItem")
            {
                int iRow = e.Item.ItemIndex;
                Session["FunctionID"] = grvData.Items[iRow]["ID"].Text.Replace("&nbsp;", "");
                Session[DetailName] = 2;
                RadWindow2.NavigateUrl = "FunctionList.aspx";
                RadWindow2.ID = "Rad1";
                RadWindow2.VisibleOnPageLoad = true;
                RadWindow2.Width = 1000;
                RadWindow2.Height = 500;
                RadWindow2.VisibleStatusbar = false;
                RadWindowManager1.Windows.Clear();
                RadWindowManager1.Windows.Add(RadWindow2);
                //ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "window.open( 'ViewDetail.aspx?ID=" + DetailID + "', null, 'height=400,width=800,status=yes,toolbar=no,menubar=no,location=no' );", true);
            }

            else if (commandName == "EditDelItem")
            {
                int iRow = e.Item.ItemIndex;
                Session["FunctionID"] = grvData.Items[iRow]["ID"].Text.Replace("&nbsp;", "");
                Session[DetailName] = 1; //0: tạo mới; 1: Sửa/Xóa; 2: Chỉ xem
                Response.Redirect("~/Function");
            }
        /*}
        catch (Exception ex)
        {

            string url = "~/Default.aspx?ID=48";
            Response.Redirect(url);
            Session["ErrorMessage"] = ex.Message;

        }*/
    }
    protected void grvData_PageIndexChanged(object sender, GridPageChangedEventArgs e)
    {
        try
        {
            LoadDatatoGrid();
        }
        catch (Exception ex)
        {

            string url = "~/Default.aspx?ID=48";
            Response.Redirect(url);
            Session["ErrorMessage"] = ex.Message;

        }
    }

    protected void grvData_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
    {
        try
        {
            LoadDatatoGrid();
        }
        catch (Exception ex)
        {

            string url = "~/Default.aspx?ID=48";
            Response.Redirect(url);
            Session["ErrorMessage"] = ex.Message;

        }
    }
    protected void grvData_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        
        try
        {
            LoadDatatoGrid();
            btnClear.Focus();
        }
        catch (Exception ex)
        {

            string url = "~/Default.aspx?ID=48";
            Response.Redirect(url);
            Session["ErrorMessage"] = ex.Message;

        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
     
        ClearForm();
        btnNew.Focus();
        
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
       
        Session[DetailName] = 0; //0: tạo mới; 1: Sửa/Xóa; 2: Chỉ xem
        Response.Redirect("~/Function");
        btnDelSelection.Focus();
    }
    protected void btnDelSelection_Click(object sender, EventArgs e)
    {
        try
        {
            bool isDelete = false;
            foreach (GridDataItem grdItem in grvData.Items)
            {
                CheckBox chkSelection = (CheckBox)grdItem["checkbox"].Controls[0];
                //In the radgrid's second cell we are binding the userid and delete  
                //the row using this unique id  
                //int userid = Convert.ToInt32(grdItem.Cells[2].Text);
                if (chkSelection.Checked == true)
                {
                    isDelete = true;
                    DeleteData(Convert.ToInt32(grdItem.Cells[3].Text));
                }
            }
            if (isDelete)
            {
                LoadDatatoGrid();
                JavascriptUtil.LoadMessage(UpdatePanel1, true, "Xoá thành công!");
                
            }
            grvData.Rebind();
        }
        catch (Exception ex)
        {

            string url = "~/Default.aspx?ID=48";
            Response.Redirect(url);
            Session["ErrorMessage"] = ex.Message;

        }
    }

    protected void DeleteData(int ID)
    {
        
        //DbParameter paramID = DB.Parameter;
        //paramID.ParameterName = "ID";
        //paramID.DbType = DbType.Int32;
        //paramID.Value = ID;
        //DB.AddParameter(paramID);

        try
        {
            //dt = DB.ExecuteDataTable("DeleteFunction", CommandType.StoredProcedure, ConnectionState.CloseOnExit);
            DBDataContext dtx = LINQUtil.GetDBWriteOnly();
            dtx.DeleteFunction(ID);
        }
        catch (Exception ex)
        {
            JavascriptUtil.LoadMessage(UpdatePanel1, false, "Thông báo lỗi: Xóa không thành công. Mã lỗi: CodeDetail - " + ex.Message);
            
        }
    }
    protected void cboParentID_SelectedIndexChanged(object sender, RadTreeNodeEventArgs e)
    {
        txtFunctionName.Focus();
    }
    protected void cboFunctionType_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        cboParentID.Focus();
    }
    protected void txtFunctionName_TextChanged(object sender, EventArgs e)
    {
        cboFunctionLevel.Focus();
    }
    protected void cboFunctionLevel_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        radAll.Focus();
    }
    protected void radAll_CheckedChanged(object sender, EventArgs e)
    {
        radSysFunction.Focus();
    }
    protected void radSysFunction_CheckedChanged(object sender, EventArgs e)
    {
        radPublicFunction.Focus();
    }
    protected void radPublicFunction_CheckedChanged(object sender, EventArgs e)
    {
        btnSearch.Focus();
    }
    
  
}